﻿Public Class frmElevesSearch
    Public Nompren As String
    Public Formule As String

    Private szSQL As String
    Dim szCritere As String
    Private rs As ADODB.Recordset

    Private Sub frmElevesSearch_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If gCD_CLI = 0 Then
            TE_NOM.Clear()
        Else
            TE_NOM.Text = Format(gCD_CLI, "000000")
        End If
        ChkCLOTURE.Checked = False

        InitListeEleves()

    End Sub

    Private Sub InitListeEleves()

        Dim nb As Integer = 0

        ' effacer la listview
        LV_ELEVES.Items.Clear()

        If TE_NOM.Text = "" Then
            ' afiche les 50 derniers ELEVES crées par l'agence
            szSQL = "CALL spGet_ELEVES_byCRITERES('" & gUserAgence & "'," & CInt(ChkCLOTURE.Checked) & ")"
        Else
            ' affiche les 50 ELEVES à partir de l'élève courant
            szSQL = "CALL spGet_ELEVES_byCRITERES('" & TE_NOM.Text & "'," & CInt(ChkCLOTURE.Checked) & ")"
        End If
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_ELEVES.Items.Add(rs.Fields("cd_cli").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("civilite").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
            LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("permis_demande").Value))
            LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("formule").Value))
            LV_ELEVES.Items(nb).SubItems.Add(Zero2Space(rs.Fields("solde").Value))
            LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("statut").Value))
            If rs.Fields("solde").Value < 0 Then
                LV_ELEVES.Items(nb).ForeColor = Color.Red
            End If
            If nv(rs.Fields("statut").Value) > 0 Then
                LV_ELEVES.Items(nb).ForeColor = Color.Gray
            End If
            ' Get the next record
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        'positionner le curseur sur la première ligne
        setIndice(0, LV_ELEVES)

    End Sub

    Private Sub LV_ELEVES_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_ELEVES.DoubleClick
        If LV_ELEVES.SelectedItems.Count > 0 Then
            ' mémoriser l'élève sélectionné
            gCD_CLI = LV_ELEVES.SelectedItems(0).Text
            Nompren = LV_ELEVES.SelectedItems(0).SubItems(2).Text
            Formule = LV_ELEVES.SelectedItems(0).SubItems(4).Text
            ' puis retour à l'appelant
            Me.Close()
        End If

    End Sub

    Private Sub LV_ELEVES_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_ELEVES.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter  ' Enter = Double-Clic
                LV_ELEVES_DoubleClick(sender, e)

        End Select

    End Sub

    Private Sub TE_NOM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' réaffiche la listview
            InitListeEleves()
        End If

    End Sub

    Private Sub ChkCLOTURE_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkCLOTURE.CheckedChanged
        ' réaffiche la listview
        InitListeEleves()
    End Sub

End Class